package gui;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDesktopPane;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.event.EventListenerList;

/**
 * Okno pro prihlaseni k serveru
 * 
 * @author dove
 * 
 */
public class LoginWin extends InternalFrame implements ActionListener,
		ItemListener {

	private static final long serialVersionUID = 1L;

	private JTextField login;

	private String vLogin;

	private JPasswordField pass;

	private JCheckBox checkSave;

	private JButton ok;

	private JButton canc;

	JDesktopPane desktop;

	private JButton guest;

	/**
	 * Nastavuje nazev okna
	 * 
	 * @param nazev
	 *            okna
	 */
	public LoginWin(String name) {
		super(name);
	}

	/**
	 * Nastavi okno
	 * 
	 * @param desktop
	 *            plocha na kterou se maji zobrazit okna po prihlaseni
	 */
	public void setWindow(JDesktopPane desktop) {
		this.desktop = desktop;
		vLogin = prefs.get("login", "");

		JPanel pan = new JPanel();
		pan.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();

		JPanel inputs = new JPanel();
		inputs.setLayout(new GridBagLayout());
		GridBagConstraints c2 = new GridBagConstraints();

		c2.insets = new Insets(10, 10, 10, 10);
		c2.gridx = 0;
		c2.gridy = 0;
		JLabel llogin = new JLabel("Login:");
		inputs.add(llogin, c2);

		c2.gridx = 1;
		login = new JTextField();
		login.setPreferredSize(new Dimension(100, 25));
		login.setText(vLogin);
		inputs.add(login, c2);

		c2.gridx = 0;
		c2.gridy = 1;
		JLabel lpass = new JLabel(messages.getString("Password"));
		inputs.add(lpass, c2);

		c2.gridx = 1;
		pass = new JPasswordField();
		pass.setPreferredSize(new Dimension(100, 25));
		pass.setText(prefs.get("password", ""));
		inputs.add(pass, c2);

		c.gridx = 0;
		c.gridy = 1;
		c.insets = new Insets(10, 10, 10, 10);
		c.gridwidth = 2;
		pan.add(inputs, c);

		c.gridx = 0;
		c.gridy = 2;
		checkSave = new JCheckBox(messages.getString("Save_name"));
		if (Boolean.valueOf(prefs.get("checkSave", "false"))) {
			checkSave.setSelected(true);
		}
		checkSave.addItemListener(this);
		pan.add(checkSave, c);

		c.gridx = 0;
		c.gridy = 3;
		c.gridwidth = 1;
		ok = new JButton(messages.getString("Ok"));
		ok.setPreferredSize(new Dimension(80, 20));
		ok.addActionListener(this);
		pan.add(ok, c);

		c.gridx = 1;
		canc = new JButton(messages.getString("Cancel"));
		canc.setPreferredSize(new Dimension(80, 20));
		canc.addActionListener(this);
		pan.add(canc, c);

		c.gridx = 0;
		c.gridy = 4;
		c.gridwidth = 2;

		guest = new JButton(messages.getString("LoginGuest"));
		guest.setPreferredSize(new Dimension(180, 20));
		guest.addActionListener(this);
		pan.add(guest, c);

		this.add(pan);

		this.pack();
	}

	public void actionPerformed(ActionEvent a) {
		if (a.getSource() == ok) {
			fireLogin(false);

			this.setVisible(false);
		}
		if (a.getSource() == guest) {
			fireLogin(true);
			this.setVisible(false);
		}
		if (a.getSource() == canc) {
			this.setVisible(false);
		}
	}

	public void itemStateChanged(ItemEvent e) {
		Object source = e.getItemSelectable();
		logger.debug(source);
		if (source == checkSave) {
			prefs.put("checkSave", "true");
			prefs.put("login", login.getText());
			prefs.put("password", String.valueOf(pass.getPassword()));
		}
		if (e.getStateChange() == ItemEvent.DESELECTED) {
			prefs.put("checkSave", "false");
			prefs.remove("login");
			prefs.remove("password");
		}
	}

	EventListenerList xxxListeners = new EventListenerList();

	public void addLoginListener(LoginListener listener) {
		xxxListeners.add(LoginListener.class, listener);
	}

	public void removeLoginListener(LoginListener listener) {
		xxxListeners.remove(LoginListener.class, listener);
	}

	protected void fireLogin(boolean guest) {
		Object[] listeners = xxxListeners.getListenerList();
		int numListeners = listeners.length;
		for (int i = 0; i < numListeners; i += 2) {
			if (listeners[i] == LoginListener.class) {
				LoginEvent e = new LoginEvent();
				if (guest) {
					e.setLogin("guest");
				} else {
					e.setLogin(login.getText());
				}
				e.setPassword(String.valueOf(pass.getPassword()));
				((LoginListener) listeners[i + 1]).connecting(e);
			}
		}
	}
}
